<?php
	include "../public/commom/config.php";//引入配置连接数据库文件
	include "../public/commom/commom.php";//引入配置图片文件
	include "../public/commom/timename.php";//引入配置时间文件
	
	if(!isset($_SESSION["user_id"])) {
		header("Location: ../index.php");//跳转页面
		exit;
	}
	
	$user_id = isset($_SESSION["user_id"]) ? $_SESSION["user_id"] : "";//用户id
	$username = isset($_SESSION["username"]) ? $_SESSION["username"] : "";//昵称
	$headportrait = isset($_SESSION["headportrait"]) ? $_SESSION["headportrait"] : "";//头像
	
	if($_GET) {
		$tuser_id = $_GET["tuser_id"];
		$tusername = $_GET["tusername"];
		
		$sql = "UPDATE privateletter SET status = '1' WHERE tuser_id = $tuser_id AND status = '0'";
			
		$result = mysqli_query($conn, $sql);
	}else {
		$tuser_id = 0;
	}
?>
<!DOCTYPE HTML>
<html>
<head>
	<meta charset="UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta name="renderer" content="webkit">
	<title>简易微博 私信</title>
	<script type="text/javascript" src="../js/jquery-1.11.3.min.js" ></script>
	<script type="text/javascript" src="../js/bootstrap.min.js"></script>
	<link rel="stylesheet" href="../css/bootstrap.min.css" />
	<link rel="stylesheet" href="../css/set.css" />
	<script type="text/javascript">
		function newmessage(tuid) {
			$.post("handle.php", {
				tuser_id: tuid
			}, function(data, status) {
				if(data.poor) {
					for(var i = data.poor - 1; i >= 0; i--) {
						var txt = "<div class='cont_oth'><div class='cont_oth_time'><p>" + data.chatlist[i]["time"] + "</p></div><div class='cont_oth_head'><img src='<?php echo $imgPath; ?>"+ data.chatlist[i]["headportrait"] + "' width='100%'/><div class='cont_oth_tent'><p>" + data.chatlist[i]["content"] + "</p></div></div></div>";
				   	 	$(".cont_private").append(txt);
					}
				}
				    
		    }, "json");
		}
		$(function () {
			setInterval("newmessage(<?php echo $tuser_id; ?>)", 2000);
		});
		
	</script>
</head>
<body>
	<?php
		if($_GET) {
			
			?>
			<div class="c_pri_tit">
				<h4><?php echo $tusername; ?></h4>
			</div>
			<div class="cont cont_private">
				<?php
					$sql = "SELECT privateletter.*,user.headportrait FROM privateletter LEFT JOIN user ON (privateletter.tuser_id = user.id) WHERE (privateletter.tuser_id = '$tuser_id' AND privateletter.user_id = '$user_id') ORDER BY time";//sql语句 - 查找该用户所有私信
					
					$result = mysqli_query($conn, $sql);//result - 执行sql语句
					
					$chatnum = mysqli_num_rows($result);//用户聊天数量
					
					$_SESSION["currentnum"] = $chatnum;//对方聊天数量
					
					$sql = "SELECT privateletter.*,user.headportrait FROM privateletter LEFT JOIN user ON (privateletter.tuser_id = user.id) WHERE (privateletter.tuser_id = '$tuser_id' AND privateletter.user_id = '$user_id') OR (privateletter.tuser_id = '$user_id' AND privateletter.user_id = '$tuser_id') ORDER BY time";//sql语句 - 查找该用户所有私信
					
					$result = mysqli_query($conn, $sql);//result - 执行sql语句
							
					while($row = mysqli_fetch_array($result)) {
						$tname = pritname($row["time"]);
						if($tuser_id == $row["tuser_id"]) {
							?>
							
								<div class="cont_oth">
									<div class="cont_oth_time">
										<p><?php echo $tname; ?></p>
									</div>
									<div class="cont_oth_head">
										<img src="<?php echo $imgPath . $row["headportrait"]; ?>" width="100%"/>
										<div class="cont_oth_tent">
											<p><?php echo $row["content"]; ?></p>
										</div>
									</div>
								</div>
							
							<?php
						}else {
							?>
							
								<div class="cont_own">
									<div class="cont_oth_time">
										<p><?php echo $tname; ?></p>
									</div>
									<div class="cont_own_head">
										<img src="<?php echo $imgPath . $row["headportrait"]; ?>" width="100%"/>
										<div class="cont_own_tent">
											<p><?php echo $row["content"]; ?></p>
										</div>
									</div>
								</div>
								
							<?php
						}
					}
				?>
				
			</div>
			
			<div class="cont cont_comment cont_rig">
				<form action="privateletter.php" method="post" enctype="multipart/form-data">
					<input type="text" class="form-control form-ter" name="content" autocomplete="off" />
					
					<div class="row">
						<div class="col-md-3 col-md-offset-9">
							<input type="hidden" name="tuser_id" value="<?php echo $tuser_id; ?>" />
							<input type="hidden" name="parameter" value="<?php echo $parameter; ?>" />
							<input type="submit" class="btn btn-primary btn-block" value="发布">
						</div>
					</div>
					
				</form>
			</div>
			
			<?php
		}
	?>
	
</body>
</html>